package com.yc.yyds.util;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@WebFilter(filterName = "EncodingFilter",value = {"/*"})
public class EncodingFilter implements Filter {
    public void init(FilterConfig config) throws ServletException {
        System.out.println("EncodingFilter 初始化");
    }

    public void destroy() {
        System.out.println("EncodingFilter 销毁");
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
        if(((HttpServletRequest) request).getServletPath().endsWith("Servlet") ||
                ((HttpServletRequest) request).getServletPath().endsWith(".s")){
            request.setCharacterEncoding("utf-8");
            response.setCharacterEncoding("utf-8");
            response.setContentType("application/json;charset=utf-8");
        }
        chain.doFilter(request, response);
        System.out.println("EncondingFliter执行后");
    }
}
